import store from '../../store'
import moment from 'moment'
import { BASE_REDIRECT_PARAM } from '~/assets/js/constant'
let baseData = {}
// 初始化配置基础数据 filterParam - 需要对dictData数据的value进行指定，
//filterType - true只取指定的value，false排除指定的value,filterType-传对象进来主要是为了有些可能存在一部分需要排除，一部分需要指定
 const initTableDictData = (list, filterParam,filterType) => {
    let dictItem = store.state.dict || []
    if(dictItem.length > 0){
        dictItem.map(item=>{
            baseData[item.type] = item.children
        })
    }
        
    let paramObj = baseData;
    let redirectObj = BASE_REDIRECT_PARAM
 
    if (redirectObj) {
        list.map(item => {
            if (redirectObj[item.prop] && paramObj[redirectObj[item.prop]]) {
                let parent = paramObj[redirectObj[item.prop]];
                if(filterParam && filterParam[item.prop]){
                    if((typeof filterType == 'boolean') && filterType){
                        item.dicData = getFilter('appoint',parent,filterParam,item.prop);
                    }else if(typeof filterType == 'object'){
                        if(filterType[item.prop]){
                            item.dicData = getFilter('appoint',parent,filterParam,item.prop);
                        }else{
                            item.dicData = getFilter('exclude',parent,filterParam,item.prop);
                        }
                    }else{
                        item.dicData = getFilter('exclude',parent,filterParam,item.prop);
                    }
                }else{
                    item.dicData = parent;
                }     
            }
        })
    }
    return list;
}
const getFilter = (type,parent,children,prop) =>{
    
    if(type == 'appoint'){
        return parent.filter((el) =>children[prop].indexOf(el.value) > -1);
    }else{
        return parent.filter((el) =>children[prop].indexOf(el.value) == -1);
    }
}
 const  dictFilter = (key,value) =>{
    if(BASE_REDIRECT_PARAM[key]){
        let dictItem = store.state.dict || []
        let baseData = {}
        if(dictItem.length > 0){
            dictItem.map(item=>{
                baseData[item.type] = item.children
            })
        }
        let list = baseData[BASE_REDIRECT_PARAM[key]] || []
        // 这里为什么不能用 === 来作比较，因为码值存的是字符串，而业务应用时有可能是number，就会导致转义不过来
        let data = list.find(item => item.value == value) || {}
        // if(key == 'gender'){
        //     console.log(list,data)
        // }
        return data.label || value
    }else{
        return value;
    }
    
}

const setLoading = (title) =>{
    return {
        lock: true,
        text: title || '加载载中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    }
}

const viewFormat = (date,type) =>{
    if(type){
        return moment().format('YYYY-MM-DD')
    }else{
        if(date){
            return moment(date).format('YYYY-MM-DD')
        }else{
            return date ;
        }
    }
    
} 
const createUUID = () => {
    let uuidRegEx = /[xy]/g
    let uuidReplacer = function (c) {
        let r = Math.random() * 16 | 0
        let v = c === 'x' ? r : (r & 3 | 8)
        return v.toString(16)
    }
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(uuidRegEx, uuidReplacer).toUpperCase()
}

// 口音核实状态排除

const ACCENT_STATUS_EXCLUSION = '0,2';

export default {
    initTableDictData,
    viewFormat,
    dictFilter,
    setLoading,
    createUUID,
    
    ACCENT_STATUS_EXCLUSION
}
